<?php
namespace App\Http\Controllers\WebApi\Home;

use App\Repositories\Home\CompanyRepository;
use Illuminate\Http\Request;

class CompanyController extends BaseController
{
    protected $com; //货单

    public function __construct()
    {
        parent::__construct();
        $this->com = new CompanyRepository();
    }

    /**
     * 搜索条件
     */
    public function getConditions(Request $request)
    {
        //校验接口权限、用户登陆权限
        $resWeb = $this->getWebAccess($request);
        if ($resWeb['code']!=0) {
            return $this->toJson($resWeb['code']);
        }
        $uid = $resWeb['data'];
        return $this->toJson(0,$this->com->getWebConditions());
    }

    /**
     * 列表
     */
    public function getIndex(Request $request)
    {
        //校验接口权限、用户登陆权限
        $resWeb = $this->getWebAccess($request);
        if ($resWeb['code']!=0) {
            return $this->toJson($resWeb['code']);
        }
        $uid = $resWeb['data'];
        //请求参数
        $cateId = $request->post('cateId',0);
        $cityId = $request->post('cityId',0);
        $yearId = $request->post('yearId',0);
        $type = $request->post('type',0);  //1公司，2工厂
        $page = $request->post('page',1);
        $pageSize = $request->post('pageSize',0);
        $dataArr = $this->com->getWebComList($cateId,$cityId,$yearId,$type,$page,$pageSize);
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司发布的产品列表
     */
    public function getBaleList(Request $request)
    {
        //校验接口权限、用户登陆权限
        $resWeb = $this->getWebAccess($request);
        if ($resWeb['code']!=0) {
            return $this->toJson($resWeb['code']);
        }
        $uid = $resWeb['data'];
        //获取历史订单列表
        $page = $request->post('page',1);
        $pageSize = $request->post('pageSize',0);
        $dataArr = $this->com->getBaleList($uid,$page,$pageSize);
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司合作过的历史订单
     */
    public function getOrderList(Request $request)
    {
        //校验接口权限、用户登陆权限
        $resWeb = $this->getWebAccess($request);
        if ($resWeb['code']!=0) {
            return $this->toJson($resWeb['code']);
        }
        $uid = $resWeb['data'];
        //获取历史订单列表
        $page = $request->post('page',1);
        $pageSize = $request->post('pageSize',0);
        $dataArr = $this->com->getHistoryOrderList($uid,$page,$pageSize);
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 公司概要
     */
    public function getAbout(Request $request)
    {
        //校验接口权限、用户登陆权限
        $resWeb = $this->getWebAccess($request);
        if ($resWeb['code']!=0) {
            return $this->toJson($resWeb['code']);
        }
        $uid = $resWeb['data'];
        //获取公司简介
        $dataArr = $this->com->getAbout($uid);
        if (!$dataArr) { return $this->toJson(-3); }
        return $this->toJson(0,$dataArr);
    }

    /**
     * 校验接口权限、用户登陆权限
     */
    public function getWebAccess(Request $request)
    {
        //接口权限
        $wa = $request->post('wa','');
        if ($wa!=$this->web_access) {
            return array('code'=>'-1');
        }
        //用户登陆
        $token = $request->post('token','');
        $uid = $this->getUidByToken($token);
        if (!$uid) {
            return array('code'=>'-2');
        }
        return array('code'=>'0', 'data'=>$uid);
    }
}